package com.wj.facedetect.widget;


import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.View;
import android.widget.TextView;

import com.daimajia.numberprogressbar.NumberProgressBar;
import com.daimajia.numberprogressbar.OnProgressBarListener;
import com.wj.facedetect.R;


public class NumberProssbarDialog extends Dialog implements OnProgressBarListener {

    private TextView tv;
    private NumberProgressBar numberProgressBar;
    private boolean cancelable = true;

    private boolean fullscreen = false;
    private Context context;
    public NumberProssbarDialog(Context context) {
        super(context, R.style.AlertDialogStyle);
        this.context = context;
        init();
    }

    public NumberProssbarDialog(Context context, boolean fullscreen) {
        super(context, R.style.AlertDialogStyle);
        this.context = context;
        init();
    }

    @SuppressWarnings("ResourceType")
    private void init() {
        View contentView = View.inflate(getContext(), R.layout.activity_custom_prossbar_dialog_layout, null);
        if (fullscreen) {
            contentView.setBackground(new ColorDrawable(android.R.color.transparent));
        }
        setContentView(contentView);
        tv = (TextView) findViewById(R.id.tv);
        numberProgressBar = findViewById(R.id.number_progress_bar);
        numberProgressBar.setReachedBarHeight(dp2px(10));
        numberProgressBar.setUnreachedBarHeight(dp2px(9));
        numberProgressBar.setOnProgressBarListener(this);

    }

    @Override
    public void show() {
        super.show();
    }

    @SuppressLint("NewApi")
    @Override
    public void dismiss() {
        super.dismiss();
    }


    @Override
    public void setCancelable(boolean flag) {
        cancelable = flag;
        super.setCancelable(flag);
    }

    @Override
    public void setTitle(CharSequence title) {
        tv.setText(title);
    }

    @Override
    public void setTitle(int titleId) {
        setTitle(getContext().getString(titleId));
    }

    @Override
    public void onProgressChange(int current, int max) {
        if (current == max) {
            dismiss();
        }
    }

    public void setNumberProgress(int progress) {
        if (numberProgressBar != null) {
            numberProgressBar.setProgress(progress);
        }
    }
    public float dp2px(float dp) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return dp * scale + 0.5f;
    }
}
